package sort;

//Comparable<Goods>位于java.lang包,不用导包
public class Goods implements Comparable<Goods>{
    private String id;
    private String name;
    private double price;

    public Goods(String id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return
                "商品编号:" + id +
                ", 商品名称:" + name +
                ", 商品价格:" + price;
    }
    //在类中直接调用接口并重写该方法
    @Override
    public int compareTo(Goods o) {
        //对商品价格进行降序排序
        //取出商品价格
        double price1=this.getPrice();
        double price2=o.getPrice();
        //int n=new Double(price2-price1).intValue();//取两个价格之差上的整数部分
        double p=price2-price1;
        if (p>0){
            return 1;
        }else if (p<0){
            return -1;
        }else {
            return 0;
        }
    }
}
